<?php
/*
 * 微信操作的基类
 */
namespace wechat;
class wx{
    protected $config = [];
    protected $message;
    
    public function __construct(array $config){
        $this->config = $config;
        $this->message = $this->getUserResponse();
        
    }
    
    public function valid(){
        if(isset($_GET["signature"]) && isset($_GET["timestamp"]) && isset($_GET["nonce"]) && isset($_GET["echostr"])){
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $token = $this->config['token'];
            $tmpArr = array($token, $timestamp, $nonce);
    		sort($tmpArr, SORT_STRING);
    		$tmpStr = implode( $tmpArr );
    		$tmpStr = sha1( $tmpStr );
    		if($tmpStr == $signature){
    		    echo $_GET["echostr"];
    		}
        }
    }
    
	public function getData(){
	    return $this->message;
	}
	
	//用户发给微信获取的数据
	private function getUserResponse(){
	    if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
	        return simplexml_load_string($GLOBALS["HTTP_RAW_POST_DATA"],'SimpleXMLElement',LIBXML_NOCDATA);
	    }
	}
}


















